<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
/**
 * Called by ZzIO, this class join all the query for children
 */
class ZzChildrenQuery {
	
	private $_needs = array();
	private $queryWasExecuted = true;
	private $_child = array();
	private $_result = array();


	/**
	 * Define all rows we need
	 * @param string $arg The row we need
	 * @param string $arg2 A second row, this one must be a row of an other table. The first row is the primary/foreing key.
	 */
	 public function need($arg,$arg2 = null) {
		if($arg2 == null) {
			if(!in_array($arg,$this->_needs)) {
				$this->_needs[] = $arg;
				$this->queryWasExecuted = false;
			}
		}
		else {
			if(!in_array(array($arg,$arg2),$this->_needs)) {
				$this->_needs[] = array($arg,$arg2);
				$this->queryWasExecuted = false;
			}
		}
	}

	/**
	 * Add a chid to the object
	 * @param int $child lid of the added child
	 */
	public function add($child) {
		$this->_child[] = $child;
	}
	/**
	 * Create and execute the query
	 */
	public function execQuery($id) {
		if($this->queryWasExecuted)
			return $this->_result[$id];
		$this->queryWasExecuted = true;
		$nbArgs = 0;
		$_tables = array(); //Liste des tables
		$query = 'SELECT  `'.$GLOBALS['prefix'].'label`.id, ';
		$resp = 2;
		$first = true;
		foreach($this->_needs as $value) {
			//if(!in_array($val,array('name','dateC','dateU','user','group','type','id'))) {
				if($first)
					$first = false;
				else
					$query .= ', ';
				if(is_array($value))
					$query .= '`'.$GLOBALS['prefix'].strToLower($value[0]).'`.`'.$value[1].'` ';
				else
					$query .= '`'.$GLOBALS['prefix'].strToLower($this->_child[0]->domain->$value->dom).'`.`'.$value.'` ';

				$nbArgs++;
				
				if(is_array($value)) {
					if(!in_array(array($this->_child[0]->domain->$value[0]->dom,$value[0],$value[0]),$_tables))
						$_tables[] = array($this->_child[0]->domain->$value[0]->dom,$value[0],$value[0]);
				}
				elseif($this->_child[0]->domain->$value->dom != 'label') {
					if(!in_array(array('label',$this->_child[0]->domain->$value->dom,'id'),$_tables))
						$_tables[] = array('label',$this->_child[0]->domain->$value->dom,'id');
				}
		}
		//Liste des id
			$first = true;
			$listId = '';
			foreach($this->_child as $child) {
				if($first) 
					$first = false;
				else
					$listId .= ' OR ';
				$listId .= '`'.$GLOBALS['prefix'].'label'.'`.id = '.$child->id;
			}
		
		//Liste des tables
		$listTable = '';
		for($i = 0; $i<count($_tables);$i++) {
			$listTable .= ' INNER JOIN `'.$GLOBALS['prefix'].strToLower($_tables[$i][1]).'` ON `'.$GLOBALS['prefix'].strToLower($_tables[$i][1]).'`.id = `'.$GLOBALS['prefix'].strToLower($_tables[$i][0]).'`.'.$_tables[$i][2].'';
		}
		$query .= 'FROM `'.$GLOBALS['prefix'].'label`'.$listTable.' WHERE '.$listId;
		
		$_var[] = $listId;

		$_rows = ZzSQL::query($query,$_var);
		foreach($_rows as $result) {
				$this->_result[$result['id']] = $result;
		}
		
		return $this->_result[$id];
	}
}
?>